<?php

class ProfileButtonColumn extends CButtonColumn {

    public $template = '{action}';
    public $buttons = array('action');

    protected function renderButton($id, $button, $row, $data) {
        if (isset($button['visible']) && !$this->evaluateExpression($button['visible'], array('row' => $row, 'data' => $data)))
            return;        
        
    	$current_user = isset($button['current_user']) ? $button['current_user']:"";
		
    	$urlProfile = url('/profile/update', array('id'=>$data->id));
    	$titleProfile = t('LBL_EDIT_PROFILE', 'profile');
    	$permissionSubmitter = array(ProfileStatus::STATUS_NEW, ProfileStatus::STATUS_REJECTED);
    	
    	$urlTranslate = url('/translate/index', array('id'=>$data->id));
    	$titleTranslate = t('LBL_TRANSLATE_PROFILE', 'profile');
    	$permissionEditor = array(ProfileStatus::STATUS_NEW, ProfileStatus::STATUS_WAITING, ProfileStatus::STATUS_TRANSLATING, ProfileStatus::STATUS_WAITING_FOR_RETRANSLATE, ProfileStatus::STATUS_TRANSLATED);

    	$urlApprove = url('/translate/detail', array('id'=>$data->id, 'alias'=>stringURLSafe($data->full_name)));
    	$titleApprove = t('LBL_APPROVE_PROFILE', 'profile');
    	$permissionManager = array(ProfileStatus::STATUS_TRANSLATED);
    	
    	$urlHistory = url('/history/profile', array('id'=>$data->id, 'alias'=>stringURLSafe($data->full_name)));
    	$titleHistory = t('PAGE_TITLE', 'history');
    	$permissionTranslator = array(ProfileStatus::STATUS_NEW, ProfileStatus::STATUS_WAITING, ProfileStatus::STATUS_TRANSLATING, ProfileStatus::STATUS_WAITING_FOR_RETRANSLATE, ProfileStatus::STATUS_TRANSLATED);
    	
    	$urlAdvanceProfile = url('/advanceprofile/index', array('id'=>$data->id, 'alias'=>stringURLSafe($data->full_name)));
    	$titleAdvanceProfile = t('PAGE_TITLE', 'advanceProfile');
    	
		if($current_user->isSubmitter() && in_array($data->status, $permissionSubmitter))
		{
			echo Html::buttonInGrid('linkEditProfile', '', $titleProfile, '', 'icon edit', '', '', array('onclick'=>'window.location.href=\''. $urlProfile .'\''));
		}
		else if ($current_user->isEditor() && in_array($data->status, $permissionEditor))
		{
			echo Html::buttonInGrid('linkTranslateProfile', '', $titleTranslate, '', 'icon translate', '', '', array('onclick'=>'window.location.href=\''. $urlTranslate .'\''));
			echo Html::buttonInGrid('linkAdvanceProfile', '', $titleAdvanceProfile, '', 'icon advanceProfile', '', '', array('onclick'=>'window.location.href=\''. $urlAdvanceProfile .'\''));			
		}
		else if ($current_user->isManager() && in_array($data->status, $permissionManager))
		{
			//echo Html::buttonInGrid('linkApproveProfile', '', $titleApprove, '', 'icon checked', '', '', array('onclick'=>'window.location.href=\''. $urlApprove .'\''));
			echo Html::buttonInGrid('linkHistoryProfile', '', $titleHistory, '', 'icon history', '', '', array('onclick'=>'window.location.href=\''. $urlHistory .'\''));
		} 
		else if($current_user->isAdmin() || $current_user->isSuperAdmin())
		{
			echo Html::buttonInGrid('linkEditProfile', '', $titleProfile, '', 'icon edit', '', '', array('onclick'=>'window.location.href=\''. $urlProfile .'\''));
			echo Html::buttonInGrid('linkTranslateProfile', '', $titleTranslate, '', 'icon translate', '', '', array('onclick'=>'window.location.href=\''. $urlTranslate .'\''));
			//if (in_array($data->status, $permissionManager)){
			//	echo Html::buttonInGrid('linkApproveProfile', '', $titleApprove, '', 'icon checked', '', '', array('onclick'=>'window.location.href=\''. $urlApprove .'\''));
			//}
			echo Html::buttonInGrid('linkAdvanceProfile', '', $titleAdvanceProfile, '', 'icon advanceProfile', '', '', array('onclick'=>'window.location.href=\''. $urlAdvanceProfile .'\''));
			echo Html::buttonInGrid('linkHistoryProfile', '', $titleHistory, '', 'icon history', '', '', array('onclick'=>'window.location.href=\''. $urlHistory .'\''));
		}
		else if($current_user->isTranslator() && in_array($data->status, $permissionTranslator))
		{
			echo Html::buttonInGrid('linkTranslateProfile', '', $titleTranslate, '', 'icon translate', '', '', array('onclick'=>'window.location.href=\''. $urlTranslate .'\''));
		}	
    }
}

?>